package cn.iocoder.yudao.module.vos.dal.mysql.ecdr;

import java.util.List;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.vos.controller.admin.ecdr.vo.ECdrPageReqVO;
import cn.iocoder.yudao.module.vos.dal.dataobject.ecdr.ECdrDO;
import cn.iocoder.yudao.module.vos.dal.dataobject.ecdr.ECdrDOWithoutTag;
import cn.iocoder.yudao.module.vos.job.vo.CdrMongoDO;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.cursor.Cursor;
import org.apache.ibatis.scripting.xmltags.XMLLanguageDriver;
import com.baomidou.dynamic.datasource.annotation.DS;

/**
 * CDR Mapper
 */
@DS("mysql2")
@Mapper
public interface ECdrMapper extends BaseMapperX<ECdrDO> {

    // 分片分页查询（你已有的 Provider）
    @SelectProvider(type = ECdrSqlProvider.class, method = "buildSelectPageScript")
    @Lang(XMLLanguageDriver.class)
    List<ECdrDO> selectPageByShards(@Param("req") ECdrPageReqVO reqVO,
                                    @Param("offset") int offset,
                                    @Param("limit") int limit);

    @SelectProvider(type = ECdrSqlProvider.class, method = "buildCountScript")
    @Lang(XMLLanguageDriver.class)
    Long countByShards(@Param("req") ECdrPageReqVO reqVO);

    /**
     * 新增：无分页全量查询（导出使用）
     */
    @SelectProvider(type = ECdrSqlProvider.class, method = "buildSelectAllScript")
    @Lang(XMLLanguageDriver.class)
    List<ECdrDO> selectAllByShards(@Param("req") ECdrPageReqVO reqVO);

    /**
     * 检查分表是否存在
     * 使用 #{ } 防止 SQL 注入
     */
    @Select("SELECT COUNT(*) " +
            "FROM information_schema.tables " +
            "WHERE table_schema = #{databases} AND table_name = #{tableName}")
    Integer checkTableExists(@Param("databases") String databases,
                             @Param("tableName") String tableName);

    /**
     * 按日分表流式读取（具体 SQL 在 XML：resultSetType=FORWARD_ONLY, fetchSize=2000, 带 ORDER BY）
     * JDBC URL 需开启：useServerPrepStmts=true&useCursorFetch=true
     */
    Cursor<CdrMongoDO> streamByDay(@Param("tableName") String tableName);

    /**
     * 便捷分页封装
     */
    default PageResult<ECdrDO> selectPage(ECdrPageReqVO reqVO) {
        int pageNo = reqVO.getPageNo() == null ? 1 : reqVO.getPageNo();
        int pageSize = reqVO.getPageSize() == null ? 10 : reqVO.getPageSize();
        int offset = (pageNo - 1) * pageSize;

        List<ECdrDO> list = selectPageByShards(reqVO, offset, pageSize);
        Long total = countByShards(reqVO);
        return new PageResult<>(list, total);
    }


    @org.apache.ibatis.annotations.Select({
            "<script>",
            "SELECT callergatewayid, holdtime, calleetogatewaye164, starttime, stoptime, ",
            "       calleeaccesse164, enddirection, callere164, calleee164, callerip, calleeip, flowno ",
            "FROM ${tableName} ",
            "WHERE holdtime &gt; #{holdTime} ",
            "ORDER BY starttime DESC ",
            "<if test='limit != null and limit &gt; 0'>",
            "  LIMIT #{limit}",
            "</if>",
            "</script>"
    })
    java.util.List<cn.iocoder.yudao.module.vos.dal.dataobject.ecdr.ECdrDOWithoutTag> listLastDayCdrByHoldTime(
            @org.apache.ibatis.annotations.Param("holdTime") int holdTime,
            @org.apache.ibatis.annotations.Param("tableName") String tableName,
            @org.apache.ibatis.annotations.Param("limit") Integer limit
    );


    List<String> listPhonesByDay(@Param("table") String table);

    List<CdrMongoDO> selectAllByDay(@Param("table") String table);
}
